package com.top.mall.main.goods

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ConvertUtils
import com.google.android.material.tabs.TabLayout
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeFragment
import com.top.mall.R
import com.top.mall.custom.SpacingItemDecoration
import com.top.mall.custom.setTransitionStatus
import com.top.mall.entity.GoodsRequest
import com.top.mall.jetpack.vm.GoodsViewModel
import com.top.mall.jetpack.vm.MineViewModel
import com.top.mall.main.goods.adapter.GoodsHomeAdapter
import kotlinx.android.synthetic.main.fragment_goods_home.*
import kotlinx.android.synthetic.main.item_tab_time.view.*

class GoodsHomeFragment : OwnerViewModeFragment<GoodsViewModel>() {

    companion object {
        private const val TYPE_ID = "TYPE_ID"
        fun newInstance(id: Int): GoodsHomeFragment {
            val bundle: Bundle = Bundle().apply {
                putInt(TYPE_ID, id)
            }
            return GoodsHomeFragment().apply {
                arguments = bundle
            }
        }
    }

    private val goodsHomeAdapter: GoodsHomeAdapter =
        GoodsHomeAdapter()

    private var mGoodsRequest: GoodsRequest = GoodsRequest()

    private val mActiveType: Int
        get() = arguments?.getInt(TYPE_ID) ?: 2

    override val layoutResId: Int
        get() = R.layout.fragment_goods_home

    override fun viewModel(): GoodsViewModel {
        return createViewModel()
    }

    override fun initView() {
        rvGoodsHomeBody.apply {
            val decoration = SpacingItemDecoration(
                nSpan = 2,
                vGap = ConvertUtils.dp2px((13f)),
                hGap = ConvertUtils.dp2px((13f))
//                isHead = getArgumentsId() == 3
            )
            addItemDecoration(decoration)
        }.adapter = goodsHomeAdapter.apply {
            //setEmptyView(createLoadingView())
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                GoodsDetailsActivity.open(itemData.id)
            }
        }
    }

    override fun initData() {
        viewModel.goodsList(mGoodsRequest.apply { active_type = mActiveType })
    }

    override fun initViewMode() {
        viewModel.goodsListLiveData.observe(viewLifecycleOwner, { response ->
            goodsHomeAdapter.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            goodsHomeAdapter.setNewInstance(data)
        })
    }

    /**
     * 创建头布局
     * */
    private fun createHeaderTime(): View {
        val layoutInflater: View = layoutInflater.inflate(R.layout.item_goods_home_heads, null)
        layoutInflater.findViewById<TabLayout>(R.id.tlGoodsHomeTime).apply {
            val tabs: MutableList<String> = mutableListOf<String>(
                "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"
            )
            for ((index: Int, item: String) in tabs.withIndex()) addTab(newTab().apply {
                val view: View = LayoutInflater.from(requireContext())
                    .inflate(R.layout.item_tab_time, null)
                view.tvTabGoodName.text = item
                customView = view
                tag = index
            })
        }
        return layoutInflater
    }

    private fun createLoadingView(): View {
        return layoutInflater.inflate(R.layout.dialog_load_style_2, null)
    }

}